//
// Created by 17775 on 2024/11/4.
//

#include "Single_Histogram.h"

std::shared_ptr<PowerProfileData<QVector<double>>> Single_Histogram::getHistogramData() const {
    bool _bHistogramValid = false;
    std::shared_ptr<PowerProfileData<QVector<double>>> histogramData;
    RayCi_Single_Histogram_View_isValid(_iSingleDocId, &_bHistogramValid);
    if(_bHistogramValid) {
        int iDataLength = 0;
        RayCi_Single_Histogram_getDataLength(_iSingleDocId, 8, &iDataLength);
        if(iDataLength != 0) {
            std::vector<float> dIntensityThresholds_buffer;
            std::vector<uint32_t> dCounts_buffer;
            dIntensityThresholds_buffer.resize(iDataLength);
            dCounts_buffer.resize(iDataLength);
            RayCi_Single_Histogram_getDataAsBinary(_iSingleDocId, 8, false, &dIntensityThresholds_buffer[0], &iDataLength, &dCounts_buffer[0], &iDataLength);
            QVector<double> vecDoubleIntensityThresholds(dIntensityThresholds_buffer.begin(), dIntensityThresholds_buffer.end());
            QVector<double> percentageData;
            percentageData.resize(iDataLength);
            std::transform(dCounts_buffer.begin(), dCounts_buffer.end(), percentageData.begin(),
                           [](const uint32_t value) {
                               return static_cast<double>(value) / 41616;
                           });
            histogramData = std::make_shared<PowerProfileData<QVector<double>>>(vecDoubleIntensityThresholds, percentageData);
        }
    }
    return histogramData;
}

void Single_Histogram::closeHistogramWindow() const {
    RayCi_Single_Histogram_Window_close(_iSingleDocId);
}
